<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\index\model\Admin as AdminModel;
use think\Loader;
use think\captcha\Captcha;
use app\index\model\Users;

class Admin extends Typez
{
    public function back(){
        if (request()->isPost()){
            $data = input('post.');
            //验证码验证码
            $captcha = new Captcha();
            if (!$captcha->check($data['captcha'])){
                return $this->error("验证码错误");
            }
            $admin = Users::get(['name' => $data['name']]);
            if (!$admin){
                $phone = Users::get(['phone' => $data['name']]);
                if (!$phone->save()){
                    $this->error('原密码错误');exit;
                }


                $phone->password = md5($data['new_password']);
                if (!$phone){
                    $this->error('修改失败');exit;
                }
            }else{
                $pwd = Users::get(['password' => md5($data['password'])]);
                if (!$pwd){
                    $this->error('原密码错误');exit;
                }

                $pwd->password = md5($data['new_password']);
                if (!$pwd->save()){
                    $this->error('修改失败');exit;
                }
            }
            $this->success('修改密码成功',url('index/admin/login'));exit;
        }
        return view('login/back');
    }

    public function edit(){
        if(request()->isPost()){
            $data = input('post.');

            // return \json(['code' => 1 , 'msg' => '添加管理员成功']);
            // die;
            // $validate = Loader::validate('User');
            // return \json(['code' => 0 , 'msg' => '1']);
            // die;
            // if (!$validate->check($data)) {
            //     return \json(['code' => 0 , 'msg' => '1']);
            // }
            $admin = new AdminModel();
            $res = $admin->edit($data);
            if ($res['valid']) {
                session(null);
                return \json(['code' => 1 , 'msg' => $res['msg']]);
            }else{
                return \json(['code' => 0 , 'msg' => $res['msg']]);
            }
            return;
        }
        return view();
    }

    public function login(){
        $admin = new AdminModel();
		if (request()->isPost()) {
		    $data = input('post.');
            $res = $admin->login($data);
            //验证码验证码
            $captcha = new Captcha();
            if (!$captcha->check($data['captcha'])){
                return $this->error("验证码错误");
            }

            if ($res['valid']) {
                $this->success($res['msg'],url('index/index/index'));exit;
            }else{
                $this->error($res['msg']);exit;
            }
            return;
            }
        return view('login/index');
    }

    public function register(){
        $admin = new AdminModel();
        if(request()->isPost()){
            $data = input('post.');
			$validate = \think\Loader::validate('Admin');
			if (!$validate->scene('add')->check($data)) {
				$this->error($validate->getError());
			}

            //验证码验证码
            $captcha = new Captcha();
            if (!$captcha->check($data['captcha'])){
                return $this->error("验证码错误");
            }

            $res = $admin->register($data);
            if($res['valid']){
                $this->success($res['msg'],url('index/admin/login'));exit;
            }
        }else{
            $this->error('注册失败');exit;
        }
        return;
    }

    public function logout(){
        //清除session
        session(null);
		$this->success('退出系统成功','index/index/index');
	}


}
